﻿var _this = this;

var popup=false;
var watchClose;

$(document).ready(function() {
//	var lang = getLanguageResource();
	console.log(1111111);
	loadAddressBookTree();
	_this.table1 = tableSync('tbl1');
	$('#userDetailView').hide();
	$('#tbl1').hide();
	$('#btn_search').click(function() {
		searchBtn($('#input_search').val());
	});
	
	$('#fileOut').click(fileOut);
	popup.closed=true;
//	logs("ready");
//	logs(JSON.stringify(lang));
//	logs(lang["text.client.addressbook.choose_department_msg"]);
});



function fileOut(){
	try{
		if(popup.closed==undefined||popup.closed){
			var url = "popup/fileOut_popup";
			popup = openCenterWindow( 400, 250, url);
			setEventPopupClosed();
		}
	}catch(e){
		errLog(e);
	}
}

function setEventPopupClosed(){
	try{
		watchClose = setInterval(function() {
			if (popup.closed) {
				clearTimeout(watchClose);
//				pezAlert("aaaaaaaaaaaa");
				location.reload();
			} 
		}, 400);	
	}catch(e){
		errLog(e);
	}
}

function searchBtn(data){
//	logs("searchBtn data="+data);
	if(data.length>0){
		$('#tbl1 tbody tr').each(function() {
			var show=false;
			var trItem = $(this);
//			logs($(this).html()+'--html()');
			$(this).find('td').each(function(){
				
//				logs($(this).attr('id')+'--id html()');
				if($(this).attr('id')=="username")
					return true;//continue
				var text = $(this).text();
//				logs(text+", "+text.indexOf(data));
				if(text.indexOf(data)>=0){
					show=true;
				}
			});
			if(show){
				trItem.show();	
			}else{
				trItem.hide();	
			}
			
		});	
	}else{
		$('#tbl1 tr').each(function() {
			$(this).show();
		});
	}
	
}


function loadAddressBookTree(){
	$("#depart_tree").bind("loaded.jstree",
			function(e, data) {
				data.inst.open_all(-1);
			}).jstree({
		"plugins" : [ "themes", "html_data", "ui" ]
	}).delegate("a", "click", function(event, data) {
		event.preventDefault();	// a tag 무시
	}).bind("select_node.jstree", function(event, data) {
		var department_code = data.rslt.obj.attr("id");
		department_getMemberInGroup(department_code);
	});
}

function department_getMemberInGroup(department_code) {
	var param = {"department_code" : department_code};
	
	$.post('clientList.json', param, function(data) {
		
		if(data.list.length==0){
			$('#tbl1 tbody').hide();
		}else{
			$('#tbl1 tbody').show();
			$('#tbl1 tr').each(function() {
				$(this).show();
			});
		}
		logs("table data.list 시작");
		logs(JSON.stringify(data.list));
		logs("table data.list 끝");
		_this.table1.tableAutoSync(data.list);
		
		$('#tbl1 tbody tr').click(function(){
			tableClickedCSS('tbl1', this);
			showUserInfo($(this).find('#username').text());
		});
		$('#tbl1').show();
		$('#tbl1_info').hide();
	});
}

function tableClickedCSS(tableId, clickedTr){
//	$('#'+tableId);
	$('#'+tableId+' tbody tr').each(function() {
		$(this).removeClass("table_clicked");
	});
	$(clickedTr).addClass("table_clicked");
}

function showUserInfo(username){
	
	var param = {"username" : username};
//	logs("showUserInfo");
	$.post('showuserinfo.json', param, function(data) {
		jsonAppend(data.dat, data.dat.department);
		jsonAppend(data.dat, data.dat.rank);
		jsonAppend(data.dat, data.dat.position);
		
		var user_birthday = data.dat.user_birthday1+"년 "+data.dat.user_birthday2+"월 "+data.dat.user_birthday3+"일";
		data.dat.user_birthday = user_birthday;
		var phone_number = data.dat.phone_number1+"-"+data.dat.phone_number2+"-"+data.dat.phone_number3;
		data.dat.phone_number = phone_number;
		var address = "("+data.dat.zipcode1+"-"+data.dat.zipcode2+") "+ data.dat.address1;
		data.dat.address= address;
		var enter_code = data.dat.enter_code1+"년 "+data.dat.enter_code2+"월 "+data.dat.enter_code3+"일";
		data.dat.enter_code = enter_code;
		
		if(data.dat.profile_img_seq==0){
			data.dat.profile_img_seq=getContextPath()+"/resources/images/no_image.jpg";
		}else{
			data.dat.profile_img_seq=getContextPath()+"/attachment/download/"+data.dat.profile_img_seq;
		}
//		logs(data.dat.profile_img_seq);
//		logs("user_kr = "+data.dat.user_kr);
		logs("data.dat 시작");
		logs(JSON.stringify(data.dat));
		logs("data.dat 끝");
		autoSync("userDetailView", data.dat);
		
		$('#userDetailView').show();
	});	
}

function department_getDepartmentInGroup(up_department_code) {
//	var param = {"groupid" : up_department_code};
//	$.post('<c:url value="/client/addressbook/_departmentInGroup.do"/>', param, function(data) {
//		$("#sl_department").html(data);
//	});
	
}

function logs(s){
	console.log(s);
}